#pragma once

#include <map>
#include <string>
#include "Tile.h"

#include "..\\..\\SGD Wrappers\\SGD_Handle.h"
#include "..\\..\\SGD Wrappers\\SGD_Geometry.h"

class TileManager
{
public:
	TileManager();
	~TileManager();

	void Render();
	void Load(std::string filePath);

	SGD::Size GetTileSize();
	SGD::Size GetTilesetSize();
	SGD::Size GetLevelSize();

	std::vector<SGD::Point> vLiquids0;
	std::vector<SGD::Point> vLiquids1;

	int GetTileHeightMap(int x, int y, int height);
	int GetTileIndex(int x, int y);

	void Clear();
	void CheckCollisions(unsigned int bucket);

	struct HeightKey
	{
		int iTileIndex;
		int iTileHeight;

		HeightKey(int index, int x)
		{
			iTileIndex = index;
			iTileHeight = x;
		}

		bool operator==(const HeightKey& key)
		{
			if (key.iTileHeight == iTileHeight && key.iTileIndex == iTileIndex)
				return true;
			else
				return false;
		}
		bool operator<(const HeightKey& key) const
		{
			if (iTileIndex < key.iTileIndex) return true;
			if (iTileIndex > key.iTileIndex) return false;
			// Indices are equal
			if (iTileHeight < key.iTileHeight) return true;
			if (iTileHeight > key.iTileHeight) return false;
			// Keys are equal
			return false;
		}
		bool operator>(const HeightKey& key) const
		{
			if (iTileIndex > key.iTileIndex) return true;
			if (iTileIndex < key.iTileIndex) return false;
			// Indices are equal
			if (iTileHeight > key.iTileHeight) return true;
			if (iTileHeight < key.iTileHeight) return false;
			// Keys are equal
			return false;
		}
	};
	struct TileKey
	{
		int iLayer;
		int iRow;
		int iCol;

		TileKey(int layer, int row, int col)
		{
			iLayer = layer;
			iRow = row;
			iCol = col;
		}

		bool operator==(const TileKey& key) const
		{
			if (iLayer == key.iLayer
				&& iRow == key.iRow
				&& iCol == key.iCol)
				return true;
			else
				return false;
		}
		bool operator<(const TileKey& key) const
		{
			if (iLayer < key.iLayer) return true;
			if (iLayer > key.iLayer) return false;
			// Layers are equal
			if (iRow < key.iRow) return true;
			if (iRow > key.iRow) return false;
			// Rows are equal
			if (iCol < key.iCol) return true;
			if (iCol > key.iCol) return false;
			// Keys are equal
			return false;
		}
		bool operator>(const TileKey& key) const
		{
			if (iLayer > key.iLayer) return true;
			if (iLayer < key.iLayer) return false;
			// Layers are equal
			if (iRow > key.iRow) return true;
			if (iRow < key.iRow) return false;
			// Rows are equal
			if (iCol > key.iCol) return true;
			if (iCol < key.iCol) return false;
			// Keys are equal
			return false;
		}
	};

private:

	std::map<HeightKey, int> mHeightMap;
	std::map<TileKey, Tile> mTiles;

	int iNumLayers;					// Number of layers
	SGD::HTexture hTileset;			// Handle to the image
	SGD::Size TileSize;				// Size of each tile
	SGD::Size TilesetSize;			// Size of the tileset (in tiles)
	SGD::Size LevelSize;			// Size of the map (in tiles)

};

